package com.bf.electroplating.controller;

import com.bf.electroplating.pojo.vo.DeviceRelationVO;
import com.bf.electroplating.service.ICompanyDeviceRelationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业设备关联关系控制器
 *
 * @author gzd
 * @since 2025-01-15
 */
@Slf4j
@RestController
@RequestMapping("/out/company/device")
@Tag(name = "企业设备关联关系", description = "企业设备关联关系相关接口")
public class CompanyDeviceRelationController {

    @Autowired
    private ICompanyDeviceRelationService companyDeviceRelationService;

    /**
     * 获取企业设备关联信息
     * 对应接口文档6.11
     */
    @PostMapping("/relation")
    @Operation(summary = "获取企业设备关联信息", description = "根据企业ID获取设备关联信息")
    public Result<List<DeviceRelationVO>> getDeviceRelations(
            @Parameter(description = "企业ID", required = true)
            @RequestBody DeviceRelationRequest request) {
        try {
            log.info("获取企业设备关联信息，企业ID：{}", request.getEnterpriseId());
            
            List<DeviceRelationVO> deviceRelations = companyDeviceRelationService
                    .getDeviceRelationsByEnterpriseId(request.getEnterpriseId().toString());
            
            return Result.success(deviceRelations);
        } catch (Exception e) {
            log.error("获取企业设备关联信息异常，企业ID：{}", request.getEnterpriseId(), e);
            return Result.error("获取企业设备关联信息失败");
        }
    }

    /**
     * 请求参数类
     */
    public static class DeviceRelationRequest {
        private Integer enterpriseId;

        public Integer getEnterpriseId() {
            return enterpriseId;
        }

        public void setEnterpriseId(Integer enterpriseId) {
            this.enterpriseId = enterpriseId;
        }
    }

    /**
     * 统一返回结果类
     */
    public static class Result<T> {
        private String code;
        private String message;
        private T data;
        private boolean success;

        public static <T> Result<T> success(T data) {
            Result<T> result = new Result<>();
            result.setCode("00000");
            result.setMessage("操作成功");
            result.setData(data);
            result.setSuccess(true);
            return result;
        }

        public static <T> Result<T> error(String message) {
            Result<T> result = new Result<>();
            result.setCode("50000");
            result.setMessage(message);
            result.setSuccess(false);
            return result;
        }

        // Getters and Setters
        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        public boolean isSuccess() {
            return success;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }
    }
}
